
#include "main.h" 

#define   DEBOUNCE			4	  /* how many times check the key every 10mS*/

INPUTS_STATE InputsState;
unsigned char near InputsEvent = 0;

/* Debounce counters */
unsigned char d1 = 0;
unsigned char d2 = 0;
unsigned char d3 = 0;
unsigned char d4 = 0;
unsigned char d5 = 0;
unsigned char d6 = 0;
unsigned char d7 = 0;
unsigned char d8 = 0;
unsigned char d9 = 0;

/* #define   LOW 	0 */
/* #define   HI	(!LOW)	 */

void Inputs_Init( void )
{
/* 	TRISF |= 0b01011110; */
/* 	TRISG |= 0b00000001; */

	TRISA |= PIN_INP_CONFIG_PORT_A;
	TRISB |= PIN_INP_CONFIG_PORT_B;
	
	TRISF |= PIN_INP_CONFIG_PORT_F;
	TRISG |= PIN_INP_CONFIG_PORT_G;
}

void Drive_Inputs ( void )
{
	InputsEvent = 0;

	if ( RC_PressedKey )			  /* Remote Control */
	{
		InputsEvent = RC_PressedKey;
		RC_PressedKey = 0;
		return ;
	}
		
	if ( Time_10ms == FALSE )
		return; 
	
   /* Input 1 */
	if ( InputsState.INP_1 == LOW )
	{
		if ( PIN_INP_1 )
		{
			if ( ++d1 > DEBOUNCE )
			{
				d1 = 0;
				InputsState.INP_1 = HIGH;
				InputsEvent = INPUT_1_UP;
			}
		}
		else
			d1 = 0;
	}
	else
	{
		if ( !PIN_INP_1 )
		{
			if ( ++d1 > DEBOUNCE )
			{
				d1 = 0;
				InputsState.INP_1 = LOW;
				InputsEvent = INPUT_1_DOWN;
				//InputsEvent = INP_1_DOWN;
			}
		}
		else
			d1 = 0;
	}

	/* Input 2 */
	if ( InputsState.INP_2 == LOW )
	{
		if ( PIN_INP_2 )
		{
			if ( ++d2 > DEBOUNCE )
			{
				d2 = 0;
				InputsState.INP_2 = HIGH;
				InputsEvent = INPUT_2_UP;
				//InputsEvent = INP_2_UP;
			}
		}
		else
			d2 = 0;
	}
	else
	{
		if ( !PIN_INP_2 )
		{
			if ( ++d2 > DEBOUNCE )
			{
				d2 = 0;
				InputsState.INP_2 = LOW;
				InputsEvent = INPUT_2_DOWN;
//				InputsEvent = INP_2_DOWN;
			}
		}
		else
			d2 = 0;
	}

	/* Input 3 */
	if ( InputsState.INP_3 == LOW )
	{
		if ( PIN_INP_3 )
		{
			if ( ++d3 > DEBOUNCE )
			{
				d3 = 0;
				InputsState.INP_3 = HIGH;
				InputsEvent = INPUT_3_UP;
//				InputsEvent = INP_3_UP;
			}
		}
		else
			d3 = 0;
	}
	else
	{
		if ( !PIN_INP_3 )
		{
			if ( ++d3 > DEBOUNCE )
			{
				d3 = 0;
				InputsState.INP_3 = LOW;
				InputsEvent = INPUT_3_DOWN;
				//InputsEvent = INP_3_DOWN;
			}
		}
		else
			d3 = 0;
	}
	
	/* Input 4 */
	if ( InputsState.INP_4 == LOW )
	{
		if ( PIN_INP_4 )
		{
			if ( ++d4 > DEBOUNCE )
			{
				d4 = 0;
				InputsState.INP_4 = HIGH;
				InputsEvent = INPUT_4_UP;
				//InputsEvent = INP_4_UP;
			}
		}
		else
			d4 = 0;
	}
	else
	{
		if ( !PIN_INP_4 )
		{
			if ( ++d4 > DEBOUNCE )
			{
				d4 = 0;
				InputsState.INP_4 = LOW;
				InputsEvent = INPUT_4_DOWN;
				//InputsEvent = INP_4_DOWN;
			}
		}
		else
			d4 = 0;
	}

	
	/* Input 5 */
	if ( InputsState.INP_5 == LOW )
	{
		if ( PIN_INP_5 )
		{
			if ( d5 > DEBOUNCE )
			{
				d5 = 0;
				InputsState.INP_5 = HIGH;
				InputsEvent = INPUT_5_UP;
				//InputsEvent = INP_0_UP;
			}
			else 
				d5++;
		}
		else
			d5 = 0;
	}
	else
	{
		if ( !PIN_INP_5 )
		{
			if ( d5 > DEBOUNCE )
			{
				d5 = 0;
				InputsState.INP_5 = LOW;
				InputsEvent = INPUT_5_DOWN;
				//InputsEvent = INP_0_DOWN;
			}
			else
				d5++;
		}
		else
			d5 = 0;
	}

	/* Input 6 (RF1) */
	if ( InputsState.INP_6 == LOW )
	{
		if ( PIN_INP_6 )
		{
			if ( d6 > DEBOUNCE )
			{
				d6 = 0;
				InputsState.INP_6 = HIGH;
				InputsEvent = INPUT_6_UP;
				//InputsEvent = INP_0_UP;
			}
			else
				d6++;
		}
		else
			d6 = 0;
	}
	else
	{
		if ( !PIN_INP_6 )
		{
			if ( d6 > DEBOUNCE )
			{
				d6 = 0;
				InputsState.INP_6 = LOW;
				InputsEvent = INPUT_6_DOWN;
				//InputsEvent = INP_0_DOWN;
			}
			else
				d6++;
		}
		else
			d6 = 0;
	}

	/* ---------------------------------------------------------------------------------------- */
	
		if ( InputsState.INP_7 == LOW )
	{
		if ( PIN_INP_7 )
		{
			if ( d7 > DEBOUNCE )
			{
				d7 = 0;
				InputsState.INP_7 = HIGH;
				InputsEvent = INPUT_7_UP;
				//InputsEvent = INP_0_UP;
			}
			else
				d7++;
		}
		else
			d7 = 0;
	}
	else
	{
		if ( !PIN_INP_7 )
		{
			if ( d7 > DEBOUNCE )
			{
				d7 = 0;
				InputsState.INP_7 = LOW;
				InputsEvent = INPUT_7_DOWN;
				//InputsEvent = INP_0_DOWN;
			}
			else
				d7++;
		}
		else
			d7 = 0;
	}

	if ( InputsState.INP_8 == LOW )
	{
		if ( PIN_INP_8 )
		{
			if ( d8 > DEBOUNCE )
			{
				d8 = 0;
				InputsState.INP_8 = HIGH;
				InputsEvent = INPUT_8_UP;
				//InputsEvent = INP_0_UP;
			}
			else
				d8++;
		}
		else
			d8 = 0;
	}
	else
	{
		if ( !PIN_INP_8 )
		{
			if ( d8 > DEBOUNCE )
			{
				d8 = 0;
				InputsState.INP_8 = LOW;
				InputsEvent = INPUT_8_DOWN;
				//InputsEvent = INP_0_DOWN;
			}
			else
				d8++;
		}
		else
			d8 = 0;
	}

	if ( InputsState.INP_9 == LOW )
	{
		if ( PIN_INP_9 )
		{
			if ( d9 > DEBOUNCE )
			{
				d9 = 0;
				InputsState.INP_9 = HIGH;
				InputsEvent = INPUT_9_UP;
				//InputsEvent = INP_0_UP;
			}
			else
				d9++;
		}
		else
			d9 = 0;
	}
	else
	{
		if ( !PIN_INP_9 )
		{
			if ( d9 > DEBOUNCE )
			{
				d9 = 0;
				InputsState.INP_9 = LOW;
				InputsEvent = INPUT_9_DOWN;
				//InputsEvent = INP_0_DOWN;
			}
			else
				d9++;
		}
		else
			d9 = 0;
	}
	
}
